package com.jerry.hm.datastructure.deque;

import java.util.Iterator;

/**
 * 基于双向环形链表实现
 *
 * @author Jerry
 * @since 2024/10/4 12:57
 */
public class DoubleLinkedListDeque<E> implements Deque<E>, Iterable<E> {

    static class Node<E> {

        /**
         * 上一个节点
         */
        Node<E> pre;

        /**
         * 下一个节点
         */
        Node<E> next;

        /**
         * 节点值
         */
        E value;

        public Node(Node<E> pre, Node<E> next, E value) {
            this.pre = pre;
            this.next = next;
            this.value = value;
        }
    }

    private Node<E> sentinel = new Node<>(null, null, null);
    private int size;
    private int capacity;

    public DoubleLinkedListDeque(int capacity) {
        this.capacity = capacity;
        sentinel.pre = sentinel;
        sentinel.next = sentinel;
    }

    /**
     * 头部添加元素
     *
     * @param e
     * @return
     */
    @Override
    public boolean offerFirst(E e) {
        if (isFull()) {
            return false;
        }

        Node<E> a = sentinel;
        Node<E> b = sentinel.next;
        Node<E> added = new Node<>(a, b, e);
        a.next = added;
        b.pre = added;
        size++;
        return true;
    }

    /**
     * 尾部添加元素
     *
     * @param e
     * @return
     */
    @Override
    public boolean offerLast(E e) {
        if (isFull()) {
            return false;
        }

        Node<E> a = sentinel.pre;
        Node<E> b = sentinel;
        Node<E> added = new Node<>(a, b, e);
        a.next = added;
        b.pre = added;
        size++;
        return true;
    }

    /**
     * 头部移除元素
     *
     * @return
     */
    @Override
    public E pollFirst() {
        if (isEmpty()) {
            return null;
        }
        Node<E> a = sentinel;
        Node<E> removed = sentinel.next;
        Node<E> b = removed.next;
        a.next = b;
        b.pre = a;
        size--;
        return removed.value;
    }

    /***
     * 尾部移除元素
     * @return
     */
    @Override
    public E pollLast() {
        if (isEmpty()) {
            return null;
        }
        Node<E> removed = sentinel.pre;
        Node<E> b = sentinel;
        Node<E> a = removed.pre;
        a.next = b;
        b.pre = a;
        size--;
        return removed.value;
    }

    /**
     * 获取头部元素
     *
     * @return
     */
    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.next.value;
    }

    /**
     * 获取尾部元素
     *
     * @return
     */
    @Override
    public E peekLast() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.pre.value;
    }

    /**
     * 是否空
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 是否满
     *
     * @return
     */
    @Override
    public boolean isFull() {
        return size == capacity;
    }

    @Override
    public Iterator<E> iterator() {

        return new Iterator<>() {

            private Node<E> cur = sentinel.next;

            @Override
            public boolean hasNext() {
                return cur != sentinel;
            }

            @Override
            public E next() {
                E value = cur.value;
                cur = cur.next;
                return value;
            }
        };
    }

}
